package com.example.lenovo.tianqi.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Switch;

import com.example.lenovo.tianqi.R;
import com.example.lenovo.tianqi.util.MySharedpreference;

/**
 * Created by lenovo on 2018/1/17.
 */

public class SettingActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private Spinner weather_spinner;
    private Spinner manager_spinner;
    private Switch mSwitch;
    private int weather_anim;
    private int manager_anim;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        editor= MySharedpreference.getInstance(SettingActivity.this);
        initView();
    }

    private void initView() {
        toolbar=findViewById(R.id.setting_toolbar);
        weather_spinner=findViewById(R.id.weather_item_anim);
        manager_spinner=findViewById(R.id.manager_item_anim);
        mSwitch=findViewById(R.id.status_bar_Is_presenter);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getSupportActionBar().setTitle("设置");
        weather_anim=MySharedpreference.preferences.getInt("Main_anim",0);
        manager_anim=MySharedpreference.preferences.getInt("Multi_anim",0);
        setSpinnerChecked(weather_anim,weather_spinner);
        setSpinnerChecked(manager_anim,manager_spinner);
        mSwitch.setChecked(MySharedpreference.preferences.getBoolean("Status_title",false));
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor.putBoolean("Status_title",isChecked);
                editor.commit();
            }
        });
        weather_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                editor.putInt("Main_anim",position);
                editor.commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        manager_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                editor.putInt("Main_anim",position);
                editor.commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void setSpinnerChecked(int anim, Spinner spinner) {
        switch (anim)
        {
            case 0:
                spinner.setSelection(0);
                break;
            case 1:
                spinner.setSelection(1);
                break;
            case 2:
                spinner.setSelection(2);
                break;
            case 3:
                spinner.setSelection(3);
                break;
            case 4:
                spinner.setSelection(4);
                break;
        }
    }
}
